import java.io.File;
import java.util.Arrays;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * 删除IDEA的试用记录，重新试用
 */
public class ResetIdea {
    public static void main(String[] args) {

        final String ideaVersion = "IntelliJIdea2021.2";

        //系统用户主目录路径
        final String userHome = System.getProperty("user.home");

        //文件目录路径，需要删除其下所有文件
        final String dirPath = userHome + "/AppData/Roaming/JetBrains/" + ideaVersion + "/eval";
        //文件路径，需要删除它
        final String filePath = userHome + "/AppData/Roaming/JetBrains/" + ideaVersion + "/options/other.xml";

        File dir = new File(dirPath);
        File file = new File(filePath);
        if (dir.exists() && dir.isDirectory()) {
            final File[] eval = dir.listFiles();
            System.out.println("获取到文件夹文件：" + Arrays.toString(eval));
            assert eval != null;
            for (File cFile : eval) {
                final boolean result = cFile.delete();
                System.out.println("删除文件夹下文件：" + cFile.getName() + (result ? "（成功）" : "（失败）"));
            }

        }
        if (file.exists() && file.isFile()) {
            final boolean result = file.delete();
            System.out.println("删除文件夹下文件：" + file.getName() + (result ? "（成功）" : "（失败）"));
        }
        final Preferences preferences = Preferences.userRoot();
        final Preferences jetbrains = preferences.node("jetbrains");
        System.out.println("删除注册表" + jetbrains.name());
        try {
            jetbrains.removeNode();
        } catch (BackingStoreException e) {
            e.printStackTrace();
        }
    }
}